package com.wang.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.exception.BusinessException;
import com.wang.reponse.EvaluationReplyVo;
import com.wang.reponse.EvaluationVo;
import com.wang.reponse.ShopVo;
import com.wang.reponse.UserVo;
import com.wang.user.model.Evaluation;
import com.wang.user.model.EvaluationReply;
import com.wang.user.model.Shop;
import com.wang.user.model.User;
import com.wang.user.service.EvaluationReplyService;
import com.wang.user.service.EvaluationService;
import com.wang.user.mapper.EvaluationMapper;
import com.wang.user.service.ShopService;
import com.wang.user.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author bkty0
* @description 针对表【evaluation(店铺评价)】的数据库操作Service实现
* @createDate 2025-04-23 10:39:21
*/
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation>
    implements EvaluationService{

    @Resource
    private UserService userService;

    @Resource
    private ShopService shopService;

    @Resource
    private EvaluationMapper evaluationMapper;

    @Resource
    private EvaluationReplyService evaluationReplyService;

    @Override
    public Boolean submit(Evaluation evaluation) {
        User loginUser = userService.getLoginUser();

        Evaluation newEvaluation = new Evaluation();
        BeanUtils.copyProperties(evaluation, newEvaluation);
        newEvaluation.setUserId(loginUser.getUid());
        return save(newEvaluation);
    }

    @Override
    public Boolean deleteById(Long id) {
        Evaluation evaluation = getById(id);
        if (evaluation == null) {
            throw new BusinessException("评价数据不存在");
        }
        return removeById(id);
    }

    @Override
    public List<EvaluationVo>  getByShopId(Long id) {
        Shop shop = shopService.getById(id);
        if(ObjectUtil.isNull(shop)){
            throw new BusinessException("商铺信息不存在");
        }
        List<Evaluation> evaluations = evaluationMapper.listByShopId(shop.getId());
        if(CollUtil.isEmpty(evaluations)){
            return Collections.emptyList();
        }
        List<Integer> userIdList = evaluations.stream().map(Evaluation::getUserId).distinct().collect(Collectors.toList());
        List<User> userList = userService.listByIds(userIdList);
        Map<Integer, User> userMap = userList.stream().collect(Collectors.toMap(User::getUid, u -> u));
        
        return evaluations.stream().map(item -> {
            EvaluationVo evaluationVo = new EvaluationVo();
            BeanUtils.copyProperties(item, evaluationVo);
            User user = userMap.get(item.getUserId());
            if(ObjectUtil.isNotNull(user)){
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user, userVo);
                evaluationVo.setUser(userVo);
            }
            
            // 获取评价的所有回复
            List<EvaluationReplyVo> replyVos = evaluationReplyService.getVoByEvaluationId(item.getId());
            evaluationVo.setReplies(replyVos);
            return evaluationVo;
        }).collect(Collectors.toList());
    }
}




